//
//  SetupViewController.m
//  UI_礼物说
//
//  Created by mac on 16/4/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SetupViewController.h"
#import "CollectCaseVC.h"
#import "CollectPhotoVC.h"
#import "ShareViewController.h"
@interface SetupViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;

@end

@implementation SetupViewController

-(void)dealloc{
    [_tableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = @"设置";
    //self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self createTableView];
    
}

-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    [_tableView release];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section) {
        return 3;
    }else if (1 == section)
        return 2;
    else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = @[@"分享应用",@"清理缓存",@"夜间模式",@"图片收藏",@"案例收藏",@"清除数据" ];
    
    UITableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.textLabel.text = array[indexPath.row];
        if (indexPath.section == 0 && indexPath.row == 2) {
            UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = switchview;
            [switchview addTarget:self action:@selector(transToNight:) forControlEvents:UIControlEventValueChanged];
            switchview.on = [USERDEFAULTS boolForKey:@"isNight"];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
        
    }
    else if (indexPath.section == 1) {
        cell.textLabel.text = array[indexPath.row +3];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else{
        cell.textLabel.text = array[indexPath.row +5];
        cell.textLabel.textColor = [UIColor redColor];
        cell.textLabel.textAlignment = 1;
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
#pragma 设置heightForFooterInSection的高 不能为0;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
-(void)transToNight:(UISwitch *)sw
{
    BOOL isNight = sw.isOn;
    if (isNight) {
        NSLog(@"进入夜间模式");
        [NOTIFICATION_CENTER postNotificationName:@"night" object:nil];
    }else{
        NSLog(@"日间模式");
        [NOTIFICATION_CENTER postNotificationName:@"day" object:nil];
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //**清除数据(数据库)*/
    if (indexPath.section == 2&&indexPath.row == 0) {
        UIAlertController *al = [UIAlertController alertControllerWithTitle:@"小主" message:@"真的要清除吗" preferredStyle:(UIAlertControllerStyleAlert)];
        [al addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action){
        [[DataBaseManager shareInstance]closeDB];
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *newPath = [path stringByAppendingPathComponent:@"my"];
        [[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
        [[DataBaseManager shareInstance]openDB];
        [[DataBaseManager shareInstance]createTable];
        [[DataBaseManager shareInstance] createTableS];
    }]];
        [al addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
        [self presentViewController:al animated:YES completion:nil];
    }
    
    //**清除catches*/
    if (indexPath.section == 0&&indexPath.row == 1) {
        UIAlertController *al = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清除缓存" preferredStyle:(UIAlertControllerStyleAlert)];
    [al addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        NSLog(@"%@",path);
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }]];
        [self presentViewController:al animated:YES completion:^{
            
        }];
    
   }
    if (indexPath.section ==0&&indexPath.row ==0) {
        ShareViewController *VC = [[ShareViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        [VC release];
    }
    if (indexPath.section == 1&&indexPath.row == 1) {
        CollectCaseVC *VC = [[CollectCaseVC alloc] init];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
        [VC release];
    }
    if (indexPath.section == 1&&indexPath.row == 0) {
        if ([[[DataBaseManager shareInstance ] selectAllphotos] count]==0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请去收藏" preferredStyle:(UIAlertControllerStyleAlert)];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        } else {
            
            CollectPhotoVC *VC = [[CollectPhotoVC alloc] init];
            VC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:VC animated:YES];
            [VC release];
        }
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
